<?php
class kst_detail_sttb_model extends MY_Model {
	var $model = 'kst_pembelian_detail';
	
	function GetJSON($as_pk_master)
	{
		$ls_sql	= "	SELECT	a.no_po,a.urutan,a.qty_order,a.kd_barang,b.nama_barang,a.no_sttb,a.qty_terima,a.harga 
					FROM	kst_pembelian_detail a
							LEFT JOIN kst_barang b ON a.kd_barang = b.kd_barang
					WHERE	a.no_po = '".$as_pk_master."' 
					ORDER BY ".(post('sort')=='' ? 'a.urutan' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
	
	function GetDetailByID($as_pk_master)
	{
		$ls_sql	= "	SELECT	a.no_po,a.urutan,a.qty_order,a.kd_barang,b.nama_barang,a.no_sttb,a.qty_terima,a.harga 
					FROM	kst_pembelian_detail a
							LEFT JOIN kst_barang b ON a.kd_barang = b.kd_barang
					WHERE	a.no_po = '".$as_pk_master."' 
					ORDER BY urutan";
		
		$result	= $this->db->query($ls_sql);
		
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return NULL;
		}
	}
	
	function Add($arr_data){
		$this->db->insert('kst_pembelian_detail', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('kst_pembelian_detail', $arr_data, $arr_rule);		
	}
	
	function GetSingle($as_roleid){
		$result = $this->db->get_where('kst_pembelian_detail', array('kd_barang' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_roleid)
	{
		$result = $this->db->get_where('kst_pembelian_detail', array('kd_barang' => $as_roleid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('kst_pembelian_detail', $arr_rule);			
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>